﻿using System.Text;
using BaseData.CommonLib.Models;
using Dapper;
using Microsoft.Data.SqlClient;
using Swimj.CommonLib.Base;
using Swimj.DataCore;

namespace BaseData.ApplicationLib.Queries;

public class WorkstationQueries : IWorkstationQueries
{
    private string? _defaultConnStr;

    public WorkstationQueries(string? defaultConnStr)
    {
        this._defaultConnStr = defaultConnStr;
    }


    public async Task<PaginatedResult<WorkstationInfoResponseDto>?> QueryWorkstationsByPage(WorkstationSearchRequestDto model)
    {
        IEnumerable<WorkstationInfoResponseDto>? data = new List<WorkstationInfoResponseDto>();
        int totalCount = 0;
        using (var db = new SqlConnection(_defaultConnStr))
        {
            var query = new Query(db);

            StringBuilder sqlBuilder = new StringBuilder(@"
select id,
       code,
       name,
       description,
       datastate,
       createdby,
       createdon,
       lastmodifiedby,
       lastmodifiedon,
       deletedby,
       deletedon
  from MES_WORKSTATION where 1 = 1 ");

            DynamicParameters parameters = new();

            if (!string.IsNullOrEmpty(model.Code))
            {
                sqlBuilder.Append(" and code like @Code ");
                parameters.Add("Code", $"%{model.Code}%");
            }

            if (!string.IsNullOrEmpty(model.Name))
            {
                sqlBuilder.Append(" and name like @Name");
                parameters.Add("Name", $"%{model.Name}%");
            }

            query.BuildPageQueries(model.Page, model.Limit, sqlBuilder.ToString());

            data = await db.QueryAsync<WorkstationInfoResponseDto>(query.SqlPage, parameters);
            //
            // var totalSqlBuilder = new StringBuilder(@"select count(1) from MES_CALCULATE_UNIT t where 1 = 1 ");
            //
            // DynamicParameters totalParameters = new();
            //
            // if (!string.IsNullOrEmpty(model.Code))
            // {
            //     totalSqlBuilder.Append(" and code like @Code ");
            //     totalParameters.Add("Code", $"%{model.Code}%");
            // }
            //
            // if (!string.IsNullOrEmpty(model.Name))
            // {
            //     totalSqlBuilder.Append(" and name like @Name");
            //     totalParameters.Add("Name", $"%{model.Name}%");
            // }
            //
            totalCount = await db.ExecuteScalarAsync<int>(query.SqCount, parameters);
        }

        return await PaginatedResult<WorkstationInfoResponseDto>.SuccessAsync(data.ToList(), totalCount,
            model.Page,
            model.Limit);
    }

    public async Task<Result<IList<WorkstationInfoResponseDto>>?> QueryWorkstations(WorkstationSearchRequestDto model)
    {
        IEnumerable<WorkstationInfoResponseDto>? data;
        using (var db = new SqlConnection(_defaultConnStr))
        {
            StringBuilder sqlBuilder = new StringBuilder(@"select id,
            code,
            name,
            description,
            datastate,
            createdby,
            createdon,
            lastmodifiedby,
            lastmodifiedon,
            deletedby,
            deletedon
                from MES_WORKSTATION where 1 = 1 ");

            DynamicParameters parameters = new DynamicParameters();

            data = await db.QueryAsync<WorkstationInfoResponseDto>(sqlBuilder.ToString(), parameters);
        }

        return await Result<IList<WorkstationInfoResponseDto>>.SuccessAsync(data.ToList());
    }

    public async Task<Result<WorkstationInfoResponseDto>> GetWorkstation(int id)
    {
        WorkstationInfoResponseDto? data;
        using (var db = new SqlConnection(_defaultConnStr))
        {
            // var db = new QueryFactory()

            StringBuilder sqlBuilder = new StringBuilder(@"select id,
            code,
            name,
            description,
            datastate,
            createdby,
            createdon,
            lastmodifiedby,
            lastmodifiedon,
            deletedby,
            deletedon
                from MES_WORKSTATION where 1 = 1 and id = @Id");


            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("Id", id);
            data = await db.QueryFirstOrDefaultAsync<WorkstationInfoResponseDto>(sqlBuilder.ToString(),
                parameters);
        }

        return await Result<WorkstationInfoResponseDto>.SuccessAsync(data);
    }
}